package com.java8.Stream;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class demo04_终止方法_1 {
    //forEach(遍历)+count(计数)
    @Test
    public void test1() {
        List<String> list = Arrays.asList("Alex", "Brian", "Charles", "David");
        //lambda表达式
        list.stream().forEach(s->System.out.println(s));
        System.out.println("-------");
        //方法引用
        list.stream().forEach(System.out::println);
        System.out.println("-------");
        //计数
        long count = list.stream().count();
        System.out.println(count);
    }
    //reduce(聚合）
    @Test
    public void test2() {
        List<Integer> num = Arrays.asList(1, 2,5, 4, 3);
        Optional<Integer> sum = num.stream().reduce((a, b) -> a + b);
        System.out.println(sum);
    }

}
